package com.sznaner.tool.util;

import android.content.Context;
import android.content.SharedPreferences;

public class DBUtil {

    private static SharedPreferences sharedPreferences;


    private static void init(){
        if(sharedPreferences == null){
            sharedPreferences =  NRTool.context().getSharedPreferences(dbName(),
                    Context.MODE_PRIVATE);
        }
    }

    public static String dbName(){
        return "global";
    }

    //String
    public static void putString(String key, String value) {
        if(key == null) return;
        if(value == null) return;
        init();
        sharedPreferences.edit().putString(key, value).commit();
    }

    public static String getString(String key) {
        return getString(null);
    }
    public static String getString(String key, String defValue) {
        if(key == null) return null;
        init();
        return sharedPreferences.getString(key, defValue);
    }


    //boolean
    public static void putBoolean(String key, boolean value) {
        if(key == null) return;;
        init();
        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key) {
        return getBoolean(key,false);
    }
    public static boolean getBoolean(String key, boolean defaultValue) {
        if(key == null) return false;
        init();
        return sharedPreferences.getBoolean(key, defaultValue);
    }


    //Int
    public static void putInt(String key, int value) {
        if(key == null) return;
        init();
        sharedPreferences.edit().putInt(key, value).commit();
    }

    public static int getInt(String key) {
        return getInt(key,0);
    }
    public static int getInt(String key, int defaultValue) {
        if(key == null) return 0;
        init();
        return sharedPreferences.getInt(key, defaultValue);
    }

    /**
     * 移除key
     */
    public static void remove(String key) {
        init();
        sharedPreferences.edit().remove(key).commit();
    }

    /**
     * 清空所有数据
     */
    public static void clear() {
        init();
        sharedPreferences.edit().clear().commit();
    }
}
